<?php

namespace App\Api\Repositories\Search;

use Illuminate\Support\Facades\Http;
use App\Models\Search\SearchExogenousData as Model;
use Exception;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;

class SearchExogenousDataRepositories
{
    protected $eloquentClass = Model::class;

    /**
     * 更新或创建搜索结果数据
     *
     * @param int $id
     * @param sting $content
     * @return void
     */
    public function update_data($id, $content)
    {
        $data = $this->eloquentClass::where('id', $id)->first();
        if ($data) {
            $data->content = $content;
            $res = $data->save();
        } else {
            $res = $this->eloquentClass::create([
                'id' => $id,
                'content' => $content,
            ]);
        }
        return $res;
    }

    public function use_id_get_content($id)
    {
        $data = $this->eloquentClass::where('id', $id)->first();
        return json_decode($data->content);
    }

    public function use_id_get_data($id)
    {
        $data = $this->eloquentClass::where('id', $id)->first();
        if (!$data) {
            throwBusinessException('无此搜索结果');
        }
        try {
//            $curl = curl_init();
//
//            curl_setopt_array($curl, array(
//                CURLOPT_URL => 'http://127.0.0.1:5000/user_detail?uid=' . $id,
//                CURLOPT_RETURNTRANSFER => true,
//                CURLOPT_ENCODING => '',
//                CURLOPT_MAXREDIRS => 10,
//                CURLOPT_TIMEOUT => 0,
//                CURLOPT_FOLLOWLOCATION => true,
//                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
//                CURLOPT_CUSTOMREQUEST => 'POST'
//            ));
//
//            $response = curl_exec($curl);
//            curl_close($curl);
            $response = Http::post('http://120.53.247.244:5000/user_detail?uid=' . $id);
        } catch (Exception $e) {
            throwBusinessException($e->getMessage());
        }
        // 处理结果数据
        $response = json_decode($response, true);
        if ($response == NULL || $response['code'] != 200) {
            throwBusinessException('数据获取失败');
        }
        return $response['info'];
    }
}
